/*
 * <Pezad Platformer>
 * Copyright 2008 Shayne Riley
 * 
 * This file is part of <Pezad Platformer>.
 * 
 * <Pezad Platformer> is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * <Pezad Platformer> is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with <Pezad Platformer>.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * To contact the authors on questions or issues regarding the license,
 * you may send an email to <shayner@gmail.com> or write to
 * Shayne Riley / P.O. Box 1092 / McCook, NE 69001.
 */

#include "SDLDelegate.h"
#include <iostream>

namespace pzd
{

SDLDelegate& SDLDelegate::getInstance()
{
	// Add a mutex here when threaded.
	// And DON'T talk to me about the check-lock-check strategy, it only works 
	// 99.999% of the time.

	// Because it is static, the destructor is called automatically at the
	// end of the application or thread or something.
	static SDLDelegate instance;

	return instance;	
}

SDLDelegate::SDLDelegate()
{
}

SDLDelegate::~SDLDelegate()
{
}

void SDLDelegate::finalize()
{
	using namespace std;

    cout << "Closing SDLDelegate...";
    cout.flush();

    static bool alreadyShutdown = false;

    if(!alreadyShutdown)
    {
        alreadyShutdown = true;
        // Shut down everything left of SDL.
        SDL_Quit();
        cout << "Done." << endl;
    }
    else
    {
        cout << "Already Done." << endl;
        return;
    }
}

bool SDLDelegate::needSDL()
{
	using namespace std;

	bool result = SDL_WasInit(SDL_INIT_VIDEO);

	// If SDL core wasn't already initialized...
	if(!result)
	{
		// ... then initialize it.
		if(SDL_Init(SDL_INIT_VIDEO) == 0)
		{
			// Success
	        cout << "SDL initialized by SDLDelegate." << endl;
	        result = true;
	    }
	    else
	    {
	    	// Failure
	        cerr << "Error: " << SDL_GetError() << endl;
	        result = false;
	    }
	}
	else
	{
		cout << "SDL already initialized." << endl;
	}

    return result;
}

bool SDLDelegate::needSDLWithWindow()
{
	using namespace std;

	// Needs SDL first. If that is not possible, then neither is the window.
	if(!needSDL())
	{
		// Failure
		cout << "SDL could not init, therefore window cannot init." << endl;
		return false;
    }

	// If window already exists, then do nothing.
	if(SDL_GetVideoSurface() != NULL)
	{
		cout << "Window already exists." << endl;
		return true;
	}

	// Create a basic one-pixel-by-one-pixel any-bpp window.
	// Look at http://docs.mandragor.org/files/Common_libs_documentation/SDL/
	// 	SDL_Documentation_project_en/sdlsetvideomode.html for more info.
	SDL_Surface *screen = SDL_SetVideoMode(1, 1, 0, 0);
	if(screen != NULL)
	{
		// Success
		SDL_WM_SetCaption(" ", 0);
		cout << "Window initialized." << endl;
	}
	else
	{
		// Failure
		cout << "Error: " << SDL_GetError() << endl;
		return false;
	}
    return true;
}

} // namespace
